package api

import (
	"gitee.com/DengAnbang/Wefore/src/bean"
	"gitee.com/DengAnbang/Wefore/src/dbops"
	"gitee.com/DengAnbang/Wefore/src/fun"
	"gitee.com/DengAnbang/goutils/httpUtils"
	"net/http"
)

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 添加权限
* @description 添加权限的接口
* @method socket type=201
* @url /pc/permission/add
* @param permissionName 必选 string 权限名称
* @param permissionCode 必选 string 权限标识代码
* @return {"code":0,"type":0,"message":"","debug_message":"","data":{"permission_id":"6FDDF84B-DFB2-494D-950C-BC0DA9BC","permission_name":"测试权限001","permission_code":"zcqx001","permission_parent_id":"0"}}
* @return_param permission_id string 权限id
* @return_param permission_name string 权限名称
* @return_param permission_code string 用户代码
* @return_param permission_parent_id string 权限父id
* @remark 最后修改时间:2019.10.29 10.30
* @number 1
 */
func PermissionAddHttp(_ http.ResponseWriter, r *http.Request) error {
	permissionName := httpUtils.GetValueFormRequest(r, "permissionName")
	permissionCode := httpUtils.GetValueFormRequest(r, "permissionCode")
	permissionParentId := httpUtils.GetValueFormRequest(r, "permissionParentId")
	permissionBean, err := dbops.PermissionAdd(permissionName, permissionCode, permissionParentId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(permissionBean)
}
func PermissionAddSocket(parameters map[string]interface{}) error {
	permissionName := fun.GetString(parameters, "permissionOldCode")
	permissionCode := fun.GetString(parameters, "permissionCode")
	permissionParentId := fun.GetString(parameters, "permissionParentId")
	permissionBean, err := dbops.PermissionAdd(permissionName, permissionCode, permissionParentId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(permissionBean)
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 修改权限
* @description 修改权限的接口(修改这个权限的标识代码或者名称)
* @method socket type=202
* @url /pc/permission/update
* @param permissionOldCode 必选 string 旧的权限标识代码
* @param permissionName 必选 string 权限名称
* @param permissionNewCode 必选 string 新的权限标识代码
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"修改成功!"}
* @remark 最后修改时间:2019.10.29 10.30
* @number 2
 */
func PermissionUpdateHttp(_ http.ResponseWriter, r *http.Request) error {
	permissionOldCode := httpUtils.GetValueFormRequest(r, "permissionOldCode")
	permissionName := httpUtils.GetValueFormRequest(r, "permissionName")
	permissionNewCode := httpUtils.GetValueFormRequest(r, "permissionNewCode")
	err := dbops.PermissionUpdate(permissionOldCode, permissionName, permissionNewCode)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("修改成功!")
}
func PermissionUpdateSocket(parameters map[string]interface{}) error {
	permissionOldCode := fun.GetString(parameters, "permissionOldCode")
	permissionName := fun.GetString(parameters, "permissionName")
	permissionNewCode := fun.GetString(parameters, "permissionNewCode")
	err := dbops.PermissionUpdate(permissionOldCode, permissionName, permissionNewCode)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("修改成功!")
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 删除权限
* @description 删除权限的接口
* @method socket type=203
* @url /pc/permission/delete
* @param permissionCode 必选 string 权限标识代码
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"删除成功"}
* @remark 最后修改时间:2019.10.29 10.30
* @number 3
 */
func PermissionDeleteHttp(_ http.ResponseWriter, r *http.Request) error {
	permissionCode := httpUtils.GetValueFormRequest(r, "permissionCode")
	err := dbops.PermissionDelete(permissionCode)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功!")
}
func PermissionDeleteSocket(parameters map[string]interface{}) error {
	permissionCode := fun.GetString(parameters, "permissionCode")
	err := dbops.PermissionDelete(permissionCode)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功!")
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 查询权限
* @description查询权限的接口
* @method socket type=204
* @url /pc/permission/select
* @param permissionCode 必选 string 权限标识代码
* @return {"code":0,"type":0,"message":"","debug_message":"","data":添加权限中的结构}
* @remark 最后修改时间:2019.10.29 10.30
* @number 4
 */
func PermissionSelectHttp(_ http.ResponseWriter, r *http.Request) error {
	permissionCode := httpUtils.GetValueFormRequest(r, "permissionCode")
	permissionBean, err := dbops.PermissionSelect(permissionCode)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(permissionBean)
}
func PermissionSelectSocket(parameters map[string]interface{}) error {
	permissionCode := fun.GetString(parameters, "permissionCode")
	permissionBean, err := dbops.PermissionSelect(permissionCode)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(permissionBean)
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 查询所有权限
* @description查询所有权限的接口
* @method socket type=205
* @url /pc/permission/select/list
* @return {"code":0,"type":0,"message":"","debug_message":"","data":[添加权限中的结构,添加权限中的结构]}
* @remark 最后修改时间:2019.10.29 10.30
* @number 5
 */
func PermissionSelectListHttp(_ http.ResponseWriter, r *http.Request) error {
	permissionBeanList, err := dbops.PermissionSelectList()
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(permissionBeanList)
}
func PermissionSelectListSocket(parameters map[string]interface{}) error {
	permissionBeanList, err := dbops.PermissionSelectList()
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(permissionBeanList)
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 添加角色的权限
* @description添加角色的权限的接口
* @method socket type=206
* @url /pc/permission/structure/add
* @param character_id 必选 string 角色的id
* @param permissionId 必选 string 权限的代码,多个权限用","隔开,比如"10018,10019"
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"权限添加成功"}
* @remark 注意:如果要添加的权限,已经有这个权限了,会忽略这一条,继续添加其他的权限
* @number 6
 */
func PermissionToStructureTreeAddHttp(_ http.ResponseWriter, r *http.Request) error {
	character_id := httpUtils.GetValueFormRequest(r, "character_id")
	permissionId := httpUtils.GetValueFormRequest(r, "permissionId")
	err := dbops.PermissionToStructureTreeAdd(character_id, permissionId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("添加成功")
}
func PermissionToStructureTreeAddSocket(parameters map[string]interface{}) error {
	character_id := fun.GetString(parameters, "character_id")
	permissionId := fun.GetString(parameters, "permissionId")
	err := dbops.PermissionToStructureTreeAdd(character_id, permissionId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("添加成功")
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 删除角色的权限
* @description查删除角色的权限的接口
* @method socket type=208
* @url /pc/permission/structure/delete
* @param character_id 必选 string 角色的id
* @param permissionId 必选 string 权限的代码,多个权限用","隔开,比如"10018,10019"
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"权限添删除成功"}
* @remark
* @number 8
 */
func PermissionToStructureTreeDeleteHttp(_ http.ResponseWriter, r *http.Request) error {
	character_id := httpUtils.GetValueFormRequest(r, "character_id")
	permissionId := httpUtils.GetValueFormRequest(r, "permissionId")
	err := dbops.PermissionToStructureTreeDelete(character_id, permissionId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}
func PermissionToStructureTreeDeleteSocket(parameters map[string]interface{}) error {
	character_id := fun.GetString(parameters, "character_id")
	permissionId := fun.GetString(parameters, "permissionId")
	err := dbops.PermissionToStructureTreeDelete(character_id, permissionId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 查询某角色的权限
* @description查询某角色的权限所拥有的权限的接口
* @method socket type=207
* @url /pc/permission/structure/select
* @param character_id 必选 string 角色的id
* @return {"code":0,"type":0,"message":"","debug_message":"","data":[添加权限中的结构,添加权限中的结构]}
* @remark 最后修改时间:2019.10.29 10.30
* @number 7
 */
func PermissionToStructureTreeSelectHttp(_ http.ResponseWriter, r *http.Request) error {
	character_id := httpUtils.GetValueFormRequest(r, "character_id")
	beans, err := dbops.PermissionToStructureTreeSelect(character_id)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}
func PermissionToStructureTreeSelectSocket(parameters map[string]interface{}) error {
	character_id := fun.GetString(parameters, "character_id")
	beans, err := dbops.PermissionToStructureTreeSelect(character_id)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 创建角色
* @description查创建角色的接口
* @method socket type=208
* @url /pc/permission/character/add
* @param name 必选 string 角色的名称
* @return {"code":0,"type":0,"message":"","debug_message":"","data":CharacterBean}
* @remark 最后修改时间:2019.10.29 10.30
* @number 8
 */
func CharacterAddHttp(_ http.ResponseWriter, r *http.Request) error {
	name := httpUtils.GetValueFormRequest(r, "name")
	err := dbops.CharacterAdd(name)
	if err != nil {
		return err
	}
	characterBean, err := dbops.CharacterSelect(name)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(characterBean)
}
func CharacterAddSocket(parameters map[string]interface{}) error {
	name := fun.GetString(parameters, "name")
	err := dbops.CharacterAdd(name)
	if err != nil {
		return err
	}
	characterBean, err := dbops.CharacterSelect(name)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(characterBean)
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 查询所有的角色
* @description查询所有的角色的接口
* @method socket type=209
* @url /pc/permission/character/select/all
* @return {"code":0,"type":0,"message":"","debug_message":"","data":[]CharacterBean}
* @remark 最后修改时间:2019.10.29 10.30
* @number 9
 */
func CharacterSelectAllHttp(_ http.ResponseWriter, r *http.Request) error {
	beans, err := dbops.CharacterSelectAll()
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}
func CharacterSelectAllSocket(parameters map[string]interface{}) error {
	beans, err := dbops.CharacterSelectAll()
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}

/**
* showdoc
* @catalog 接口文档/权限相关
* @title 删除角色
* @description 删除角色的接口
* @method socket type=210
* @url /pc/permission/character/delete
* @param character_id 必选 string 角色的id
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"删除成功"}
* @remark 最后修改时间:2019.10.29 10.30
* @number 10
 */
func CharacterDeleteHttp(_ http.ResponseWriter, r *http.Request) error {
	name := httpUtils.GetValueFormRequest(r, "name")
	err := dbops.CharacterDelete(name)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}
func CharacterDeleteSocket(parameters map[string]interface{}) error {
	name := fun.GetString(parameters, "name")
	err := dbops.CharacterDelete(name)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}
